package com.wgts.wgts.react.load;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;

import com.wgts.wgts.react.ReactConstant;
import com.wgts.wgts.react.update.ToolsUtil;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.regex.Pattern;

public class LoadUtil {

    private static LoadUtil loadUtil = null;

    private static Context context = null;


    private LoadUtil(Context context){
        this.context = context.getApplicationContext();
    }

    public static LoadUtil getInstance(Context context){
        if(loadUtil == null){
            loadUtil = new LoadUtil(context);
        }
        return loadUtil;
    }

    /**
     * 获取APK版本号
     * @param context
     * @return
     * @throws PackageManager.NameNotFoundException
     */
    public static String getAPKVersion(Context context) throws PackageManager.NameNotFoundException {
        PackageManager manager = context.getPackageManager();
        PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
        return info.versionName;
    }

    /**
     * 获取versionlist
     * @return
     */
    public static String[] getVersionList(String jsBasePath){
        ArrayList<String> result = new ArrayList<>();
        File file = new File(jsBasePath);
        String[] fileList = file.list();
        Pattern pattern = Pattern.compile("^[0-9]{8}[a-zA-Z]?$");
        if(fileList != null){
            for(String filename : fileList){
                if(pattern.matcher(filename).find()){
                    result.add(filename);
                }
            }
        }

        String[] resultString = new String [result.size()];
        for(int i = 0; i < result.size(); i++){
            resultString[i] = result.get(i);
        }

        return resultString;
    }



    /**
     * 加载JS版本决策, 若无合适的版本或本地版本出现问题，返回error，加载最后阶段会验证文件是否存在，不存在则加载内置版本
     * @param context
     * @return
     */
    public static String loadJSVersion(Context context, String jsVersionPath){
        SharedPreferences sharedPreferences = context
                .getSharedPreferences(ReactConstant.SHARED_PREFERENCES, Context.MODE_PRIVATE);
        String jsVersion = sharedPreferences.getString(ReactConstant.JS_VERSION_NAME, "0");
        String loadVersion = null;
        if(jsVersion.equals(ReactConstant.VERSION_ERROR)){
            loadVersion = "error";
        }else if(jsVersion.equals(ReactConstant.PLATFROM_ERROR)){
            List<String> noActionVersion = getNoActionJS("");
            if(noActionVersion.isEmpty()){
                loadVersion = "error";
            }else{
                loadVersion = noActionVersion.get(0);
            }
        }else {
            //若服务器下发加载版本本地没有，表示强制升级，先加载之前的版本
            if(checkJSversion(jsVersion, jsVersionPath)){
                loadVersion = jsVersion;
            }else{
                List<String> noActionVersion = getNoActionJS("");
                if(noActionVersion.isEmpty()){
                    loadVersion = "error";
                }else{
                    loadVersion = noActionVersion.get(0);
                }

            }
        }
        if(loadVersion.equals("error")){
            List<String> noActionVersion = getNoActionJS("");
            loadVersion = noActionVersion.get(0);
        }
        return loadVersion;
    }

    /**
     * 决策版本
     * @param jsversion， 剔除某个版本决策，版本本身有问题，不参与决策
     * @return
     */
   /* public static String decisionVersion(String jsversion){
        List<String> list = getNoActionJS(jsversion);
        if(list == null){
            return "";
        }
        if(list.size() != 0){
            try {
                if(ToolsUtil.CheckDetail(list.get(0))){
                    return list.get(0);
                }else{
                    decisionVersion(list.get(0));
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                decisionVersion(list.get(0));
            }
        }
        return "";
    }*/

    /**
     * 剔除某个版本后取出JS列表中的非活动页面, 若无和是版本，则返回内置版本
     * @return
     */
    public static List<String> getNoActionJS(String jsversion){
        String[] jsList = getVersionList(ReactConstant.BASE_PATH);
        ArrayList<String> noAction = new ArrayList<>();
        if(jsList.length != 0){
            for(String js : jsList){
                if(js.length() == 8 && !js.equals(jsversion)){
                    noAction.add(js);
                }
            }
            Collections.sort(noAction, new Comparator<String>() {
                @Override
                public int compare(String lhs, String rhs) {
                    return rhs.compareTo(lhs);
                }
            });
            return noAction;
        }
        Collections.sort(noAction, new Comparator<String>() {
            @Override
            public int compare(String lhs, String rhs) {
                return rhs.compareTo(lhs);
            }
        });
        return noAction;
    }

    /**
     * 下发版本是否存在与版本列表中
     */
    public static boolean checkJSversion(String loadingVersion, String jsVersionPath){
        String[] list = getVersionList(jsVersionPath);
        if(list.length == 0){
            return false;
        }
        for(String js : list){
            if(js.equals(loadingVersion)){
                return true;
            }
        }
        return false;
    }

    /**
     * 返回Assets文件的加载路径
     * @return
     */
    public static String assetsVersion(Context context, String jsVersionPath){

        String jsVersion = null;

        try {
            String fileNames[] = context.getAssets().list(jsVersionPath);
            if(fileNames.length == 1){
                jsVersion = fileNames[0];
            }else{
                for(String file : fileNames){
                    if(file.length() == 8){
                        jsVersion =  file;
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return jsVersion;
    }


    /**
     * 渠道获取
     * @param context
     * @return
     */
    public static String getChannel(Context context) {
        try {
            PackageManager pm = context.getPackageManager();
            ApplicationInfo appInfo = pm.getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);
            return appInfo.metaData.getString("UMENG_CHANNEL");
        } catch (PackageManager.NameNotFoundException ignored) {
        }
        return "";
    }

}
